import sys

import pygame
from pygame.sprite import Sprite

import global_var
from game_functions.render import show_start_screen


def check_events(ship: Sprite, start_screen: Sprite) -> None:
    """监控用户键盘操作，返回是否需要重置游戏"""
    for event in pygame.event.get():
        match event.type:
            case pygame.QUIT:
                pygame.quit()
                sys.exit()

            case pygame.KEYDOWN:
                if event.key == pygame.K_q:  # 按下q键退出游戏
                    pygame.quit()
                    sys.exit()

                elif event.key == pygame.K_p:  # 按下p键暂停游戏，再次按下p键恢复游戏
                    if global_var.get_game_state() == 'living':
                        global_var.set_game_state('paused')
                    elif global_var.get_game_state() == 'paused':
                        global_var.set_game_state('living')

                elif event.key in (pygame.K_RIGHT, pygame.K_LEFT, pygame.K_UP, pygame.K_DOWN):
                    handle_ship_movement(ship, event.key, True)

                elif event.key == pygame.K_SPACE:  # 按下空格键不松开持续发射子弹
                    global_var.set_value('sustain_fire', True)

                elif event.key == pygame.K_RETURN:  # 按下回车键进入游戏
                    global_var.set_value('show_start_screen', False)

            case pygame.KEYUP:
                if event.key in (pygame.K_RIGHT, pygame.K_LEFT, pygame.K_UP, pygame.K_DOWN):
                    handle_ship_movement(ship, event.key, False)
                elif event.key == pygame.K_SPACE:
                    global_var.set_value('sustain_fire', False)

            case pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                start_screen.check_about_click(mouse_pos)


def handle_ship_movement(ship: Sprite, key, is_pressed) -> None:
    """统一处理飞船移动相关按键事件"""
    move_states = {
        pygame.K_RIGHT: (ship.MOVE_RIGHT, 'moving_right'),
        pygame.K_LEFT: (ship.MOVE_LEFT, 'moving_left'),
        pygame.K_UP: (ship.MOVE_UP, 'moving_up'),
        pygame.K_DOWN: (ship.MOVE_DOWN, 'moving_down')
    }

    move_state, attr_name = move_states[key]  # 元组解包
    if is_pressed:
        ship.MOVE_STATE = move_state
    setattr(ship, attr_name, is_pressed)  # setattr python内置函数，用于设置对象的属性值


def begin_start_screen(ship: Sprite, start_screen: Sprite) -> None:
        """开启开始屏幕"""
        while global_var.get_value('show_start_screen'):
            check_events(ship,start_screen)
            show_start_screen(start_screen)
            if global_var.get_value('show_about_screen'):
                # show_about_screen
                print("show about screen")
            global_var.set_value('running', True)




# 旧的实现方法，使用match语句
# def check_events(ship: object) -> None:
#     """监控用户键盘操作"""
#     for event in pygame.event.get():
#         match event.type:
#             case pygame.QUIT:
#                 pygame.quit()
#                 sys.exit()
#
#             case pygame.KEYDOWN:  # 按下键盘
#                 if event.key == pygame.K_q:
#                     pygame.quit()
#                     sys.exit()
#                 elif event.key == pygame_menu.events.RESET:
#                     print('Play Again')
#                 elif event.key == pygame.K_RIGHT:
#                     ship.MOVE_STATE = ship.MOVE_RIGHT
#                     ship.moving_right = True
#                 elif event.key == pygame.K_LEFT:
#                     ship.MOVE_STATE = ship.MOVE_LEFT
#                     ship.moving_left = True
#                 elif event.key == pygame.K_UP:
#                     ship.MOVE_STATE = ship.MOVE_UP
#                     ship.moving_up = True
#                 elif event.key == pygame.K_DOWN:
#                     ship.MOVE_STATE = ship.MOVE_DOWN
#                     ship.moving_down = True
#                 elif event.key == pygame.K_SPACE:
#                     sustain_fire_flag = True
#                     global_var.set_value('sustain_fire', sustain_fire_flag)
#                 elif event.key == pygame.K_RETURN:
#                     show_start_screen_flag = False
#                     global_var.set_value('show_start_screen', show_start_screen_flag)
#
#             case pygame.KEYUP:  # 松开键盘
#                 if event.key == pygame.K_RIGHT:
#                     ship.moving_right = False
#                 elif event.key == pygame.K_LEFT:
#                     ship.moving_left = False
#                 elif event.key == pygame.K_UP:
#                     ship.moving_up = False
#                 elif event.key == pygame.K_DOWN:
#                     ship.moving_down = False
#                 elif event.key == pygame.K_SPACE:
#                     sustain_fire_flag = False
#                     global_var.set_value('sustain_fire', sustain_fire_flag)
